<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/4/15
  Time: 9:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script src="<%=request.getContextPath()%>/js/jquery-3.2.1.min.js"></script>
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <button class="layui-btn" id="back">返回</button>
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>用户名</th>
                            <th width="250">描述</th>
                        </thead>
                        <tbody class="x-cate">

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>


    layui.use(['form'], function(){
        form = layui.form;
    });

    // 分类展开收起的分类的逻辑
    $(function(){
        //获取地址栏中的参数
        let userId = getQueryVariable("userId");
        //通过Ajax发送请求到后台
        $.ajax({
            url:"userInfo/queryDetail?id="+userId,
            type:"get",
            success:function(res){
                let data = res.data;
                console.log(data)
                //角色列表
                let roleList = data.roles;

                //添加一级列表-用户名
                $("tbody.x-cate").append(
                    "<tr cate-id='"+data.id+"' fid='0'><td><i class='layui-icon x-show' status='false'>&#xe625;</i>"+
                    data.username+"</td></tr>"
                );
                //添加二级列表--角色列表
                $.each(roleList,function(index,role){
                    $("tbody.x-cate").append(
                        "<tr cate-id='"+role.id+"' fid='"+data.id+"'><td>&nbsp;&nbsp;&nbsp;&nbsp;<i class='layui-icon x-show' status='false'>&#xe625;</i>"+
                        role.roleName+"</td><td>"+role.roleDesc+"</td></tr>"
                    );

                    //添加三级列表--权限列表
                    $.each(role.permissions,function(index,permission){
                        $("tbody.x-cate").append(
                            "<tr cate-id='"+permission.id+"' fid='"+role.id+"'><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i class='layui-icon x-show' status='true'>&#xe623;</i>"+
                            permission.permissionName+"</td><td>"+permission.url+"</td></tr>"
                        );
                    })
                })
            }
        })
        $("tbody.x-cate tr[fid!='0']").hide();
        // 栏目多级显示效果
        $('table').on('click','.x-show',function () {
            if($(this).attr('status')=='true'){
                $(this).html('&#xe625;');
                $(this).attr('status','false');
                cateId = $(this).parents('tr').attr('cate-id');
                $("tbody tr[fid="+cateId+"]").show();
            }else{
                cateIds = [];
                $(this).html('&#xe623;');
                $(this).attr('status','true');
                cateId = $(this).parents('tr').attr('cate-id');
                getCateId(cateId);
                for (var i in cateIds) {
                    $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
                }
            }
        });

        //返回按钮事件
        $('#back').click(function(){
            history.back();
        })
    })

    var cateIds = [];
    function getCateId(cateId) {
        $("tbody tr[fid="+cateId+"]").each(function(index, el) {
            id = $(el).attr('cate-id');
            cateIds.push(id);
            getCateId(id);
        });
    }
    function getQueryVariable(variable)
    {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }
</script>
</body>
</html>
